package com.book.lizhi.read;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.PopupWindow;
import android.widget.TextView;
import cn.waps.AppConnect;
import cn.waps.UpdatePointsNotifier;

import com.book.lizhi.AppApplication;
import com.book.lizhi.R;
import com.book.lizhi.constants.Constants;
import com.book.lizhi.ui.AdvActivity;
import com.book.lizhi.ui.UserInfoActivity;
import com.book.lizhi.util.AppLog;
import com.book.lizhi.util.Util;

public class ConsumeMenu extends PopupWindow {
	private View mContentView;
	private Context mContext;
	private View mAnchorView;
	private TextView mBookNameView;
	private TextView mChapterNameView;
	private TextView mPointsView;
	private TextView mCurPointsView;
	private View mRefreshView;
	private Handler mHandler;

	private CheckBox mCheckBox;
	private TextView mConsumeView;

	private int mPoints;

	public ConsumeMenu(Context context, View anchorView, Handler handler) {
		setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
		setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
		mContext = context;
		mAnchorView = anchorView;
		mHandler = handler;
		initUI();
	}

	private void initUI() {
		mContentView = View.inflate(mContext, R.layout.consume_layout, null);
		mCheckBox = (CheckBox) mContentView.findViewById(R.id.auto_get_checkbox);

		mConsumeView = (TextView) mContentView.findViewById(R.id.get_points_view);
		mBookNameView = (TextView) mContentView.findViewById(R.id.consume_book_name);
		mChapterNameView = (TextView) mContentView.findViewById(R.id.consume_chapter_name_view);
		mPointsView = (TextView) mContentView.findViewById(R.id.consume_points_view);
		mCurPointsView = (TextView) mContentView.findViewById(R.id.current_points_view);
		mRefreshView = mContentView.findViewById(R.id.refresh_points_view);
		setContentView(mContentView);
		addOnListener();
	}

	private void addOnListener() {
		mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				AppLog.e("isAuto:" + isChecked);
				Util.saveDataBySharePreference(Constants.AUTO_GET, isChecked);
			}
		});
		mConsumeView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (AppApplication.instance.points - mPoints >=0) {
					mHandler.sendEmptyMessage(ReadActivity.LOAD_NEXT_CHAPTER);
				} else {
					dismiss();
					mContext.startActivity(new Intent(mContext, AdvActivity.class));
				}
			}
		});
		mRefreshView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				AppConnect.getInstance(mContext).getPoints(new UpdatePointsNotifier() {

					@Override
					public void getUpdatePointsFailed(String arg0) {

					}

					@Override
					public void getUpdatePoints(String arg0, final int arg1) {
						v.post(new Runnable() {

							@Override
							public void run() {
								AppApplication.instance.points = arg1;
								mCurPointsView.setText(arg1 + "分");
								if(AppApplication.instance.points>=mPoints){
									mConsumeView.setText("使用积分");
								}else{
									mConsumeView.setText("获取积分");
								}
							}
						});
					}
				});
			}
		});
	}

	public void showConsumerMenu(String bookName, String chapterName, String points) {
		mPoints = Integer.parseInt(points);
		if ((AppApplication.instance.points - mPoints) >= 0) {
			mConsumeView.setText("使用积分");
		} else {
			mConsumeView.setText("获取积分");
		}
		mCheckBox.setChecked(Util.getAutoGetSharePreference(Constants.AUTO_GET));
		mCurPointsView.setText(AppApplication.instance.points + "分");
		mBookNameView.setText("书籍名称:" + bookName);
		mChapterNameView.setText("消费章节:" + chapterName);
		mPointsView.setText("所需积分:" + points);
		showAtLocation(mAnchorView, Gravity.BOTTOM, 0, 0);
	}

}
